package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.pojo.TbAddress;
import com.dongdongshop.pojo.TbAreas;
import com.dongdongshop.pojo.TbCities;
import com.dongdongshop.pojo.TbProvinces;
import com.dongdongshop.service.AddressService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@DubboService
public class AddressServiceImpl implements AddressService {
    @Resource
    private TbAddressMapper addressMapper;
    @Resource
    private TbProvincesMapper provincesMapper;
    @Resource
    private TbCitiesMapper citiesMapper;
    @Resource
    private TbAreasMapper areasMapper;


    @Override
    public List<TbAddress> findList(String username) {

        List<TbAddress> addressList = addressMapper.selectByUser(username);
        for (TbAddress tbAddress : addressList) {
            tbAddress.setProvinceId(provincesMapper.selectByPid(tbAddress.getProvinceId()));
            tbAddress.setCityId(citiesMapper.selectNameByPid(tbAddress.getCityId()));
            tbAddress.setTownId(areasMapper.selectNameByAid(tbAddress.getTownId()));
        }
        return addressList;
    }

    @Override
    public List<TbProvinces> selectProvince(Integer provinceId) {
        return provincesMapper.selectByExample(null);
    }

    @Override
    public List<TbCities> selectCities(Integer provinceId) {
        return citiesMapper.selectByProvinceId(provinceId);
    }

    @Override
    public List<TbAreas> selectAreas(Integer citiesId) {
        return areasMapper.selectByCityId(citiesId);
    }

    @Override
    public Boolean saveInfo(TbAddress address) {
        return addressMapper.insertSelective(address)>0;
    }

    @Override
    public Boolean deleteInfo(Long id) {
        return addressMapper.deleteByPrimaryKey(id)>0;
    }

    @Override
    public TbAddress selectById(Long id) {
        return addressMapper.selectByPrimaryKey(id);
    }
}
